package com.ml.mall.service.impl;

import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryOneReq;
import com.ml.mall.bean.req.admin.variant.VariantQueryOneReq;
import com.ml.mall.bean.req.purchase.PurchaseSupplierQueryPageReq;
import com.ml.mall.bean.req.purchase.PurchaseVariantQueryPageReq;
import com.ml.mall.bean.res.admin.PurchaseQueryOneRes;
import com.ml.mall.bean.res.admin.PurchaseReportPageRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryOneRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryTopRes;
import com.ml.mall.bean.res.admin.vo.AdminOverviewSalesByCategoryVO;
import com.ml.mall.bean.res.purchase.PurchaseOverviewRes;
import com.ml.mall.bean.res.purchase.PurchaseSpentQueryReportRes;
import com.ml.mall.bean.res.purchase.PurchaseSupplierQueryReportRes;
import com.ml.mall.bean.res.purchase.PurchaseVariantQueryPageRes;
import com.ml.mall.bean.res.purchase.vo.PurchaseSupplierVO;
import com.ml.mall.bean.res.purchase.vo.PurchaseVariantVO;
import com.ml.mall.bean.vo.DateValueVO;
import com.ml.mall.consts.OverviewTopConsts;
import com.ml.mall.consts.UserRoleTypeConsts;
import com.ml.mall.conversion.AnalyticsAdminServiceHelp;
import com.ml.mall.conversion.AnalyticsPurchaseServiceHelp;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.AnalyticsAdminService;
import com.ml.mall.service.AnalyticsPurchaseService;
import com.ml.mall.service.AnalyticsSupplierService;
import com.ml.mall.service.asyn.AnalyticsAdminServiceAsyn;
import com.ml.mall.service.asyn.AnalyticsPurchaseServiceAsyn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;

/**
 * Created by kyyang on 2021/7/4 12:24
 */
@Service
public class AnalyticsPurchaseServiceImpl implements AnalyticsPurchaseService {

    @Autowired
    private AnalyticsPurchaseServiceAsyn analyticsPurchaseServiceAsyn;

    @Autowired
    private AnalyticsAdminServiceAsyn analyticsAdminServiceAsyn;

    @Autowired
    private AnalyticsSupplierService analyticsSupplierService;

    @Autowired
    private AnalyticsAdminService analyticsAdminService;


    @Override
    public PurchaseOverviewRes getPurchaseOverview(BaseDateRangeReq req) throws Exception{
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            //按照分类 top N
            Future<List<AdminOverviewSalesByCategoryVO>> adminOverviewSalesByCategoryVOFuture = analyticsAdminServiceAsyn.selectSalesByCategoryTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), null,currentUser.getCompanyId());
            //按照卖家 top N
            Future<List<PurchaseSupplierVO>> purchaseSupplierVOFuture = analyticsPurchaseServiceAsyn.selectSalesBySupplierTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), currentUser.getCompanyId());
            //按照sku top N
            Future<List<PurchaseVariantVO>> purchaseVariantVOFuture = analyticsPurchaseServiceAsyn.selectSalesByVariantTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), currentUser.getCompanyId());

            return AnalyticsPurchaseServiceHelp.getPurchaseOverviewConversion(adminOverviewSalesByCategoryVOFuture,purchaseSupplierVOFuture,purchaseVariantVOFuture);

        }
        return null;
    }

    @Override
    public PurchaseSpentQueryReportRes getPurchaseSpentQueryReport(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();

        //查询订单报告
        PurchaseQueryOneRes purchaseQueryOneRes = purchaseOrderQueryReport(req, currentUser.getCompanyId());
        //按照分类 top N
        Future<List<DateValueVO>> purchaseVariantVOFuture = analyticsPurchaseServiceAsyn.getPurchaseSpentQueryReportByCategoryTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), currentUser.getCompanyId());

        return AnalyticsPurchaseServiceHelp.getPurchaseSpentQueryReportConversion(purchaseQueryOneRes,purchaseVariantVOFuture,req);
    }

    @Override
    public List<PurchaseSupplierQueryReportRes> getPurchaseSupplierQueryReport(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            Future<List<DateValueVO>> purchaseSupplierVOFuture = analyticsPurchaseServiceAsyn.getPurchaseSupplierQueryReport(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N, req.getTimeBegin(), req.getTimeEnd(), currentUser.getCompanyId());
            return AnalyticsPurchaseServiceHelp.getPurchaseSupplierQueryReportConversion(purchaseSupplierVOFuture,req);
        }
        return Collections.emptyList();
    }

    @Override
    public Page<PurchaseSupplierVO> getPurchaseSupplierQueryPage(PurchaseSupplierQueryPageReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            Page<PurchaseSupplierVO> res = new Page<PurchaseSupplierVO>(req.getPageIndex(), req.getPageNum());
            int count = 0;
            Future<Integer> countFuture = analyticsPurchaseServiceAsyn.selectPurchaseSupplierQueryPageCount(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getSupplierCode(), currentUser.getCompanyId());
            if (countFuture != null) {
                count = countFuture.get();
            }
            res.setTotal(count);
            if (count > 0) {
                Future<List<PurchaseSupplierVO>> purchaseReportPageResFuture = analyticsPurchaseServiceAsyn.selectPurchaseSupplierQueryPageList(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getSupplierCode(), req.getStart(), req.getEnd(), currentUser.getCompanyId());
                if (purchaseReportPageResFuture != null) {
                    List<PurchaseSupplierVO> purchaseReportPageRes = purchaseReportPageResFuture.get();
                    res.setRecords(purchaseReportPageRes);
                }
            }
            return res;
        }
        return null;
    }

    @Override
    public Page<PurchaseVariantQueryPageRes> purchaseVariantQueryPage(PurchaseVariantQueryPageReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            Page<PurchaseVariantQueryPageRes> res = new Page<PurchaseVariantQueryPageRes>(req.getPageIndex(), req.getPageNum());
            int count = 0;
            Future<Integer> countFuture = analyticsPurchaseServiceAsyn.selectPurchaseVariantQueryPageCount(req.getTimeBegin(), req.getTimeEnd(),  req.getSupplierCode(), currentUser.getCompanyId(),req.getCategoryId(),req.getVariantName());
            if (countFuture != null) {
                count = countFuture.get();
            }
            res.setTotal(count);
            if (count > 0) {
                Future<List<PurchaseVariantQueryPageRes>> purchaseReportPageResFuture = analyticsPurchaseServiceAsyn.selectPurchaseVariantQueryPageList(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getSupplierCode(), req.getStart(), req.getEnd(), currentUser.getCompanyId(),req.getCategoryId(),req.getVariantName());
                if (purchaseReportPageResFuture != null) {
                    List<PurchaseVariantQueryPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                    res.setRecords(purchaseReportPageRes);
                }
            }
            return res;
        }
        return null;
    }

    @Override
    public List<VariantQueryTopRes> purchaseVariantQueryTop(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            List<VariantQueryTopRes> adminCategoryQueryReportPageResPage = analyticsAdminService.adminVariantQueryTop(req,null);
            return adminCategoryQueryReportPageResPage;
        }
        return Collections.emptyList();
    }

    @Override
    public VariantQueryOneRes purchaseVariantQueryOne(VariantQueryOneReq req)throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_4 == currentUser.getRoleType()) {
            VariantQueryOneRes adminCategoryQueryReportPageResPage = analyticsAdminService.adminVariantQueryOne(req, null);
            return adminCategoryQueryReportPageResPage;
        }
        return null;
    }

    @Override
    public PurchaseQueryOneRes purchaseOrderQueryReport(BaseDateRangeReq reqt, Long supplierId) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        //查询时间范围内 买家订单 订单数
        Future<List<DateValueVO>> purchaseOrderArrFuture = analyticsAdminServiceAsyn.selectPurchaseOrderByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), currentUser.getCompanyId(), supplierId);
        //查询时间范围内 买家订单花费金额
        Future<List<DateValueVO>> purchaseSpentArrFuture = analyticsAdminServiceAsyn.selectPurchaseSpentByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), currentUser.getCompanyId(), supplierId);
        PurchaseQueryOneReq req = new PurchaseQueryOneReq();
        req.setTimeBegin(reqt.getTimeBegin());
        req.setTimeEnd(reqt.getTimeEnd());
        return AnalyticsAdminServiceHelp.adminPurchaseQueryOneDetailConversion(req, purchaseOrderArrFuture, purchaseSpentArrFuture);
    }
}
